/**
 * 202503011-day3
 * 这题写过：移动零——双指针（优选算法）
 */
//搜索插入位置
//https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-liked
public class No3 {
    public static void main(String[] args) {
        //
    }
}

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int mid = left + (right - left) / 2;
        while (left <= right) {//需要注意细节处理：可能导致死循环
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] < target) {
                left = mid + 1;
                mid = left + (right - left) / 2;
            }
            if (nums[mid] > target) {
                right = mid - 1;
                mid = left + (right - left) / 2;
            }
        }
    }
}
